package com.glory.good_dark.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.entity.PPAKInfoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: PPAKBinInfoAdapter
 * Author: XJH
 * Date: 2020/8/28 13:58
 * Description:
 * History:
 */
public class PPAKBinInfoAdapter extends BaseQuickAdapter<PPAKInfoEntity.BinInfoEntity, BaseViewHolder> {
    private List<String> binDesc ;
    private List<String> binGroup ;

    public PPAKBinInfoAdapter(@Nullable List<PPAKInfoEntity.BinInfoEntity> data) {
        super(R.layout.adapter_ppak_bin,data);
        binDesc = new ArrayList<>();
        binGroup = new ArrayList<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, PPAKInfoEntity.BinInfoEntity item) {
        helper.setText(R.id.tvPosition,item.getBIN_PROMPT());
        helper.setText(R.id.tvBinPromptDesc,item.getBIN_PROMPT_DESC());
        helper.setText(R.id.tvBinGroup,item.getBIN_GROUP());
        helper.setText(R.id.etValue,String.valueOf(item.getQuantity()));
        setEditListener(helper.getView(R.id.etValue),item);
    }

    public void setBinDescList(List<PPAKInfoEntity.BinInfoEntity> list){
        binDesc.clear();
        for (PPAKInfoEntity.BinInfoEntity entity : list){
            binDesc.add(entity.getPrompt());
        }
    }

    public void setBinGroupList(List<PPAKInfoEntity.BinInfoEntity> list){
        binGroup.clear();
        for (PPAKInfoEntity.BinInfoEntity entity : list){
            binGroup.add(entity.getGroupName());
        }
    }

    private void setEditListener(EditText editText, PPAKInfoEntity.BinInfoEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setQuantity(s.toString());
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
